/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myapp.struts.User.EditProfile;

import com.myapp.model.usuarios.Usuarios;
import com.myapp.struts.Administrator.UserAdmin.EditUserAdminForm;
import com.myapp.svc.User.UserSvc;
import java.io.File;
import java.io.FileOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

/**
 *
 * @author jaime
 */
public class EditProfileOk extends org.apache.struts.action.Action {

    /*
     * forward name="success" path=""
     */
    private static final String SUCCESS = "success";

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        HttpSession session = request.getSession();
        Usuarios usr2 = new Usuarios();
        Usuarios usr = new Usuarios();
        usr2 = (Usuarios) session.getAttribute("usuario");
        UserSvc usrSvc = new UserSvc();
        usr.setIdusuario(((EditProfileForm) form).getIdUser());
        usr.setAlias(((EditProfileForm) form).getLogin());
        usr.setPassword(((EditProfileForm) form).getPassword());
        usr.setNombre(((EditProfileForm) form).getName());
        usr.setApellidos(((EditProfileForm) form).getSurname());
        usr.setEmail(((EditProfileForm) form).getEmail());
        usr.setUbicacion(((EditProfileForm) form).getLocation());
        usr.setDispcorregir((((EditProfileForm) form).getAvailability()));
        usr.setEstados(usr2.getEstados());
        
        
        //desde aqui añadimos para subir imagenes    
        EditProfileForm fileUploadForm = (EditProfileForm)form;
        FormFile file = fileUploadForm.getAvatar();
	//Get the servers upload directory real path name
	String filePath = 
        getServlet().getServletContext().getRealPath("/") +"avatars";
	//create the upload folder if not exists
	File folder = new File(filePath);
	if(!folder.exists())
        {
	    folder.mkdir();
	}  
	String fileName = file.getFileName();
	if(!("").equals(fileName))
        {  
           System.out.println("Server path:" +filePath);
           File newFile = new File(filePath, fileName);
 
           if(!newFile.exists())
           {
	    FileOutputStream fos = new FileOutputStream(newFile);
	    fos.write(file.getFileData());
	    fos.flush();
	    fos.close();
	   }        
           request.setAttribute("uploadedFilePath",newFile.getAbsoluteFile());
	   request.setAttribute("uploadedFileName",newFile.getName());
        }
         String trayectoria = "/"+fileName;
         usr.setAvatar(trayectoria);
        // hasta aqui para subir imagenes
        
        
        
        usr = usrSvc.newOrUpdateUser(usr,usr2, false);
        session.setAttribute("usuario", usr);
        return mapping.findForward(SUCCESS);
    }
}
